<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Entity\UserEntity;
use Illuminate\Database\Eloquent\Model;
use App\Models\User;
use Validator;
use App\Http\Controllers\BaseController;
use App\Http\Lib\UserLib;

class UserController extends BaseController
{
    protected $userEntity;
    
    public function __construct(UserEntity $userEntity) {
        $this->userEntity = $userEntity;
    }
    
    # 获取用户信息, 包括用户的权限列表
    public function userinfo(Request $request)
    {
//         $uid = \Session::get('uid', 0);
//         $r = \Cache::get("userinfo:{$uid}");
        $user = $this->userEntity->getModel();
        $key = 'userinfo:'.$user->_id;
        $r = \Cache::get($key);
        if ( $r )
            return $r;
        return  $this->e('用户信息失效, 请重新登录');
    }
    
    public function currentUser() {
        $user = $this->userEntity->getModel();
        return $this->respondWithSuccess('ok', $user->toArray());
    }

    # 便于开发和测试的快速登录接口
    public function loginAs(Request $request)
    {
        if ( env('APP_ENV') !== 'product' ) {
            \Session::put('uid', $request->uid);
            //dd(\Session::get('uid'));
            $user = UserLib::getLoginUser();           
            if(!$user)  return $this->e('设置登录用户失败');
            return $this->respondWithSuccess('ok', $user->toArray());
        } else {
            return $this->e('此接口不能在生产环境调用');
        }
    }
    

}
